﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Galaktika.BI.Silverlight.Controls.PivotGrid.View;

namespace Galaktika.BI.Silverlight.Controls.PivotGrid.Controls
{
    public class CellClickEventArgs : EventArgs
    {
        public CellClickEventArgs(
            CellView cell)
        {
            this.Cell = cell;
        }

        public CellClickEventArgs(
            CellView cell, Point point)
            : this(cell)
        {
            this.Position = point;
        }

        public readonly CellView Cell = null;
        public readonly Point Position = default(Point);
    }

    public partial class CellControl : UserControl
    {
        public readonly CellView Cell = null;
        public readonly int RowIndex = -1;
        public readonly int ColumnIndex = -1;

        public CellControl(CellView cell, int columnIndex, int rowIndex)
        {
            if (cell == null)
                throw new ArgumentNullException("cell");

            RowIndex = rowIndex;
            ColumnIndex = columnIndex;

            Cell = cell;

            InitializeComponent();
            this.Loaded += new RoutedEventHandler(CellControl_Loaded);
            this.GotFocus += new RoutedEventHandler(SpanCellControl_GotFocus);
            this.MouseLeftButtonUp += new MouseButtonEventHandler(CellControl_MouseLeftButtonUp);
        }

        public event EventHandler<CellClickEventArgs> ShowContextMenu;
        private void Raise_ShowContextMenu(CellClickEventArgs args)
        {
            EventHandler<CellClickEventArgs> handler = this.ShowContextMenu;
            if (handler != null)
            {
                handler(this, args);
            }
        }

        void CellControl_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
            {
                Raise_ShowContextMenu(new CellClickEventArgs(this.Cell, e.GetPosition(null)));
            }
        }

        void SpanCellControl_GotFocus(object sender, RoutedEventArgs e)
        {
            
        }

        void CellControl_Loaded(object sender, RoutedEventArgs e)
        {
            Border border = LayoutRoot;
            
            border.CornerRadius = new CornerRadius(3);

            ShowBorder();

            // Если ячейка редактируемая
            if(Cell.IsUpdateable)
            {
                border.Background = new SolidColorBrush(Colors.Yellow); 
            }

            ShowValue();
        }

        private void ShowBorder()
        {
            if (IsFocused)
            {
                Border border = LayoutRoot;
                border.BorderBrush = new SolidColorBrush(Colors.Black);
                border.BorderThickness = new Thickness(1, 1, 1, 1);
            }
            else
            {
                Border border = LayoutRoot;
                border.BorderBrush = new SolidColorBrush(Colors.DarkGray);
                border.BorderThickness = new Thickness(0, 0, 1, 1);
            }   
        }

        private void ShowValue()
        {
            Border border = LayoutRoot;

            StackPanel root = new StackPanel();
            root.VerticalAlignment = VerticalAlignment.Center;
            //            Grid root = new Grid();
            border.Child = root;

            // Значение
            TextBlock caption_text = new TextBlock();
            caption_text.TextAlignment = TextAlignment.Right;
            caption_text.VerticalAlignment = VerticalAlignment.Center;
            caption_text.Margin = new Thickness(0, 0, 3, 0);

            if (Cell.CellDescr != null && Cell.CellDescr.Value != null)
            {
                caption_text.Text = Cell.CellDescr.Value.DisplayValue;
            }

            root.Children.Add(caption_text);
        }

        bool m_IsFocused = false;
        public bool IsFocused
        {
            set {
                m_IsFocused = value;
                ShowBorder();
            }
            get{
                return m_IsFocused;
            }
        }

        bool m_IsEditing = false;
        public bool IsEditing
        {
            set
            {
                m_IsEditing = value;
            }
            get
            {
                return m_IsEditing;
            }
        }
    }
}
